﻿using System.Diagnostics;
using System.Threading;

namespace Pixeldyne.Core.SystemResources
{
	public class SystemResourceInfo
	{
		static volatile SystemResourceInfo instance;
		static readonly object lockable = new object();

		readonly PerformanceCounter cpuCounter;
		readonly PerformanceCounter ramCounter;


		private SystemResourceInfo()
		{
			cpuCounter = new PerformanceCounter
							{
								CategoryName = "Processor",
								CounterName = "% Processor Time",
								InstanceName = "_Total"
							};


			ramCounter = new PerformanceCounter("Memory", "Available MBytes");

			//			cpuCounter.NextValue();
			ramCounter.NextValue();

			Thread.Sleep(500);
		}

		public static SystemResourceInfo Instance
		{
			get
			{
				if (instance == null)
				{
					lock (lockable)
					{
						if (instance == null)
						{
							instance = new SystemResourceInfo();
						}
					}
				}

				return instance;
			}
		}

		public float CPULoad
		{
			get
			{
				return cpuCounter.NextValue();
			}
		}

		public float AvailableMemory
		{
			get
			{
				return ramCounter.NextValue();
			}
		}

	}
}
